import { ProTableColumn } from '@/components/pro-table/type'
import { NButton, NPopconfirm } from 'naive-ui'
import { PurchaseModel } from '@/api/model/workflowpurchase-model'
import { Ref } from 'vue'

type Props = {
  handleEditPurchase: (row: PurchaseModel, type: 'edit' | 'check') => void
  fetchDelPurchase: (id: number) => void
  typeOptions: Ref<any>
  budgetOptions: Ref<any>
}

export const usePurchaseProTable = ({
  handleEditPurchase,
  fetchDelPurchase,
  typeOptions,
  budgetOptions
}: Props) => {
  const columns = computed<ProTableColumn<PurchaseModel>[]>(() => {
    return [
      {
        title: '采购编号',
        key: 'purchaseCode',
        query: {
          type: 'input'
        }
      },
      {
        title: '采购名称',
        key: 'purchaseName',
        query: {
          type: 'input'
        }
      },
      {
        title: '采购金额',
        key: 'purchaseAmount'
      },
      {
        title: '申请人',
        key: 'purchasePerson',
        query: {
          type: 'input'
        }
      },
      {
        title: '申请部门',
        key: 'purchaseBranch',
        query: {
          type: 'input'
        }
      },
      {
        title: '申请日期',
        key: 'purchaseDate'
      },
      // {
      //   title: '申请原因',
      //   key: 'purchaseReason'
      // },
      {
        type: 'dict',
        title: '预算名称',
        key: 'budgetId',
        dictOptions: budgetOptions.value,
        query: {
          type: 'select',
          dictOptions: budgetOptions.value
        }
      },
      // {
      //   title: '备注',
      //   key: 'remark'
      // },
      {
        type: 'tag',
        title: '采购状态',
        key: 'purchaseStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      {
        title: '操作',
        key: 'operation',
        render(row) {
          return (
            <>
              {row.purchaseStatus !== '2' ? (
                <NButton
                  type="primary"
                  size="small"
                  onClick={() => handleEditPurchase(row, 'edit')}
                  quaternary
                >
                  修改
                </NButton>
              ) : (
                ''
              )}

              {row.purchaseStatus === '2' ? (
                <NButton
                  type="primary"
                  size="small"
                  onClick={() => handleEditPurchase(row, 'check')}
                  quaternary
                >
                  详情
                </NButton>
              ) : (
                ''
              )}

              {row.purchaseStatus !== '2' ? (
                <NPopconfirm onPositiveClick={() => fetchDelPurchase(row.id)}>
                  {{
                    trigger: () => (
                      <NButton size="small" type="error" quaternary>
                        删除
                      </NButton>
                    ),
                    default: () => '您确定要删除？'
                  }}
                </NPopconfirm>
              ) : (
                ''
              )}
            </>
          )
        }
      }
    ]
  })

  return {
    columns
  }
}
